package com.zgjkhis.modules.system.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.poi.excel.ExcelFileUtil;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.enums.WarehousingStatusEnum;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.DrugWareHousing;
import com.zgjkhis.modules.system.domain.Store;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingDetailVO;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingVo;
import com.zgjkhis.modules.system.service.DrugWareHousingDetailService;
import com.zgjkhis.modules.system.service.DrugWareHousingService;
import com.zgjkhis.modules.system.service.StoreService;
import com.zgjkhis.modules.system.service.dto.DrugQueryCriteria;
import com.zgjkhis.modules.system.service.dto.DrugWareHousingQueryCriteria;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "库房管理：药品入库")
@RestController
@RequestMapping("/drugWareHousing")
public class DrugWareHousingController {

    private static final String ENTITY_NAME = "durgWareHouing";
    private final DrugWareHousingService drugWareHousingService;
    private final DrugWareHousingDetailService drugWareHousingDetailService;
    private final StoreService storeService;

    public DrugWareHousingController(DrugWareHousingService drugWareHousingService,
                                     DrugWareHousingDetailService drugWareHousingDetailService,
                                     StoreService storeService) {
        this.drugWareHousingService = drugWareHousingService;
        this.drugWareHousingDetailService = drugWareHousingDetailService;
        this.storeService = storeService;
    }

    @Log("新增或修改药品入库")
    @ApiOperation("新增药品入库")
    @PostMapping(value = "/add")
    public ResponseEntity<Object> create(@Validated @RequestBody DrugWareHousingVo resources) {
        return new ResponseEntity<>(drugWareHousingService.createDrugWareHousing(resources), HttpStatus.OK);
    }


    @Log("查看药品入库")
    @GetMapping(value = "/detail")
    @ApiOperation("查看药品入库")
    public ResponseEntity<Object> getDetail(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        DrugWareHousing drugWareHousing = drugWareHousingService.getById(id);
        DrugWareHousingVo drugWareHousingVo = new DrugWareHousingVo();
        BeanUtil.copyProperties(drugWareHousing, drugWareHousingVo);
        List<DrugWareHousingDetailVO> list = drugWareHousingDetailService.queryList(drugWareHousing.getWareHousingNo());
        drugWareHousingVo.setList(list);
        Store store = storeService.getById(drugWareHousingVo.getPharmacyId());
        drugWareHousingVo.setStoreName(store.getStoreName());

        return new ResponseEntity<>(drugWareHousingVo, HttpStatus.OK);
    }

    @Log("取消药品入库")
    @ApiOperation("取消药品入库")
    @PostMapping(value = "/update")
    public ResponseEntity<Object> update(String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("入库id不能为空");
        }
        DrugWareHousing drugWareHousing = drugWareHousingService.getById(id);
        if (CharUtil.isEmpty(drugWareHousing)) {
            throw new ServiceException("入库单子不存在");
        }
        if (drugWareHousing.getStatus().equals(WarehousingStatusEnum.YRK.getCode())) {
            throw new ServiceException("订单状态处于不可以取消");
        }
        DrugWareHousing wareHousing = new DrugWareHousing();
        wareHousing.setId(drugWareHousing.getId());
        wareHousing.setStatus(WarehousingStatusEnum.YQX.getCode());
        wareHousing.setCheckUserName(LoginUserUtil.getNickName());
        wareHousing.setCheckUserId(SecurityUtils.getUserId().toString());
        wareHousing.setCheckTime(new Date());
        return new ResponseEntity<>(drugWareHousingService.updateById(wareHousing), HttpStatus.OK);
    }

    @Log("分页查询入库信息")
    @ApiOperation("分页查询入库信息")
    @GetMapping(value = "/getList")
    public ResponseEntity<PageResult<DrugWareHousing>> getList(DrugWareHousingQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(drugWareHousingService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

    @Log("导出入库清单信息")
    @ApiOperation("导出入库清单信息")
    @GetMapping(value = "/outDetail")
    public void outDetail(DrugWareHousingQueryCriteria criteria, HttpServletResponse response) throws IOException {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        drugWareHousingService.outDetail(criteria, response);
    }

    @Log("导出入库信息")
    @ApiOperation("导出入库信息")
    @GetMapping(value = "/outList")
    public void outList(DrugWareHousingQueryCriteria criteria, HttpServletResponse response) throws IOException {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        drugWareHousingService.findAllByPageableForOut(criteria, response);
    }

    @Log("导出入库模版")
    @ApiOperation("导出入库模版")
    @GetMapping(value = "/outTemplate")
    public void outTemplate(DrugQueryCriteria criteria, HttpServletResponse response) throws IOException {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        drugWareHousingService.outTemplate(criteria, response);
    }

    @Log("入库导入")
    @ApiOperation("入库导入")
    @PostMapping(value = "/inTemplate")
    public ResponseEntity<Object> inTemplate(@RequestParam("file") MultipartFile file, @ApiParam("状态") Integer status, @ApiParam("入库方式") Integer wareHousingType, @ApiParam("供应商id") Long supplierId, @ApiParam("供应商名称") String supplierName, @ApiParam("药品类型") Integer drugType) throws IOException, ParseException {
        if (file == null) {
            throw new ServiceException("请上传文件");
        }
        if (!ExcelFileUtil.isXls(file.getInputStream()) && !ExcelFileUtil.isXlsx(file.getInputStream())) {
            throw new ServiceException("请上传excel文件");
        }
        drugWareHousingService.inTemplate(file, status, wareHousingType, supplierId, supplierName, drugType);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("修改支付状态")
    @ApiOperation("修改支付状态")
    @GetMapping(value = "/updatePayStatus")
    public ResponseEntity<Object> updatePayStatus(Long id, int payStatus) {
        this.drugWareHousingService.updatePayStatus(id, payStatus);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


    @Log("导出采购单")
    @ApiOperation("导出采购单")
    @GetMapping(value = "/outWareHousing")
    public void outWareHousing(String id, HttpServletResponse response) throws IOException {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        drugWareHousingService.outWareHousing(id, response);
    }


}
